package com.jhf.skypac_pda.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jhf.skypac_pda.AGVStockInActivity;
import com.jhf.skypac_pda.EventMessage.EventMessage;
import com.jhf.skypac_pda.ExceptionStockInActivity;
import com.jhf.skypac_pda.FeedingOrderListActivity;
import com.jhf.skypac_pda.FinishedProductStockInActivity;
import com.jhf.skypac_pda.HxApplication;
import com.jhf.skypac_pda.R;
import com.jhf.skypac_pda.base.BaseFragment;
import com.jhf.skypac_pda.db.impl.PermissionDaoImp;
import com.jhf.skypac_pda.model.DialogCallback;
import com.jhf.skypac_pda.model.JsonCallback;
import com.jhf.skypac_pda.model.LzyResponse;
import com.jhf.skypac_pda.model.UserListModel;
import com.jhf.skypac_pda.utils.ConstantPara;
import com.jhf.skypac_pda.utils.PrefUtils;
import com.jhf.skypac_pda.utils.Urls;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okgo.OkGo;
import okgo.model.HttpHeaders;
import okgo.model.Response;
import okhttp3.RequestBody;

import static com.jhf.skypac_pda.utils.ConstantPara.JSON;
import static okgo.utils.HttpUtils.runOnUiThread;

/**
 * Created by fhapp on 2019/6/27.
 */

public class WorkbenchFragment extends BaseFragment {
    @BindView(R.id.txt_count)
    TextView txt_count;
    private ArrayList<UserListModel> mList = new ArrayList<>();
    private ArrayList<String> mPermissionList = new ArrayList<>();

    public static WorkbenchFragment newInstance() {
        return new WorkbenchFragment();
    }

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.fragment_workbench);
    }

    @Override
    protected void setUpView(View view) {
        /*btn_cabinet_login = (Button) view.findViewById(R.id.login_button_cabinet);
        txt_cabinet_code = (TextView) view.findViewById(R.id.txt_cabinet_code);
        txt_today = (TextView) view.findViewById(R.id.txt_today);
        txt_wait = (TextView) view.findViewById(R.id.txt_wait);
        txt_overtime = (TextView) view.findViewById(R.id.txt_overtime);

        txt_overtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), OrderListActivity.class);
                intent.putExtra("type", "overtime");
                startActivity(intent);
            }
        });*/
    }

    @Override
    protected void setUpComponent() {
    }

    @Override
    protected void setUpData() {

    }

    @OnClick({R.id.ll_feeding, R.id.ll_stock_in, R.id.ll_exception, R.id.ll_agv_stock_in, R.id.ll_base_material_stock_in, R.id.ll_blown_film_stock_in, R.id.ll_manual_stock_in})
    public void onViewClicked2(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.ll_feeding:
                if (!PermissionDaoImp.getInstance(getActivity()).isHavePermission(ConstantPara.PDA_MATERIALTANKFEEDIINGORDER_LIST)) {
                    Toast.makeText(getActivity(), "您没有此权限", Toast.LENGTH_LONG).show();
                    return;
                }
                intent = new Intent(getActivity(), FeedingOrderListActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_stock_in:
                intent = new Intent(getActivity(), FinishedProductStockInActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_exception:
                intent = new Intent(getActivity(), ExceptionStockInActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_agv_stock_in:
                intent = new Intent(getActivity(), AGVStockInActivity.class);
                startActivity(intent);
                break;
             case R.id.ll_base_material_stock_in:
                intent = new Intent(getActivity(), AGVStockInActivity.class);
                intent.putExtra("stockInType", ConstantPara.STOCK_IN_TYPY_BASE_MATERIAL);
                startActivity(intent);
                break;
             case R.id.ll_blown_film_stock_in:
                intent = new Intent(getActivity(), AGVStockInActivity.class);
                intent.putExtra("stockInType", ConstantPara.STOCK_IN_TYPY_BLOWN_FILM);
                startActivity(intent);
                break;
             case R.id.ll_manual_stock_in:
                intent = new Intent(getActivity(), AGVStockInActivity.class);
                intent.putExtra("stockInType", ConstantPara.STOCK_IN_TYPY_MANUAL);
                startActivity(intent);
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShutDown(EventMessage eventMsg) {
       /* String msg = eventMsg.getMessage();
        if (msg.equals("LoginCabSuccess")) {
            btn_cabinet_login.setText(getString(R.string.login_cab_success_tip));
            txt_cabinet_code.setText(BaseApplication.getIntance().getLoginCabModel().getCabinetcode());
        }*/
    }

    @Override
    public void onResume() {
        super.onResume();
        HttpHeaders headers = new HttpHeaders();
        headers.put("Authorization", PrefUtils.getString(getActivity(), "token", ""));
        OkGo.<LzyResponse<String>>get(Urls.URL_GET_COUNT)//
                .tag(this)
                .headers(headers)
                .execute(new JsonCallback<LzyResponse<String>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<String>> response) {
                        if (null != response.body().data) {
                            //CountModel countModel = response.body().data;
                            txt_count.setText(response.body().data);
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<String>> response) {
                        Toast.makeText(getActivity(), response.getException().getMessage(),Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
